<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="引言：本文描述了我对于”为何请求总是延时5s”问题的分析、解决过程.  1. 初识最近公司项目遇到一个比较奇怪的问题，现象是在我们的系统（centos6）中发的请求包，总是需要延时5s才能成功. 一想到超时，第一感觉应该就是网络差，于是通过curl www.baidu.com验证，发现也是延时5s才返回，因此第一个结论：网络差 为了进一步确认，尝试在同一网络下其他PC机（Windows）上访问百">
<meta property="og:type" content="article">
<meta property="og:title" content="为何请求总是延时5s">
<meta property="og:url" content="http://smallmuou.gitee.io/blog/2017/03/10/2017-03-10-为何请求总是延时5s/index.html">
<meta property="og:site_name" content="smallmuou&#39;s blog">
<meta property="og:description" content="引言：本文描述了我对于”为何请求总是延时5s”问题的分析、解决过程.  1. 初识最近公司项目遇到一个比较奇怪的问题，现象是在我们的系统（centos6）中发的请求包，总是需要延时5s才能成功. 一想到超时，第一感觉应该就是网络差，于是通过curl www.baidu.com验证，发现也是延时5s才返回，因此第一个结论：网络差 为了进一步确认，尝试在同一网络下其他PC机（Windows）上访问百">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://smallmuou.gitee.io/images/dns-interaction.png">
<meta property="og:image" content="http://static.coolnull.com/wp-content/uploads/2015/05/v3.png">
<meta property="og:updated_time" content="2020-04-16T01:15:21.943Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="为何请求总是延时5s">
<meta name="twitter:description" content="引言：本文描述了我对于”为何请求总是延时5s”问题的分析、解决过程.  1. 初识最近公司项目遇到一个比较奇怪的问题，现象是在我们的系统（centos6）中发的请求包，总是需要延时5s才能成功. 一想到超时，第一感觉应该就是网络差，于是通过curl www.baidu.com验证，发现也是延时5s才返回，因此第一个结论：网络差 为了进一步确认，尝试在同一网络下其他PC机（Windows）上访问百">
<meta name="twitter:image" content="http://smallmuou.gitee.io/images/dns-interaction.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://smallmuou.gitee.io/blog/2017/03/10/2017-03-10-为何请求总是延时5s/"/>





  <title>为何请求总是延时5s | smallmuou's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">smallmuou's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"><a href="https://github.com/smallmuou">https://github.com/smallmuou</a></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/03/10/2017-03-10-为何请求总是延时5s/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">为何请求总是延时5s</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-10T00:00:00+08:00">
                2017-03-10
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>引言：本文描述了我对于”为何请求总是延时5s”问题的分析、解决过程.</p>
</blockquote>
<h2 id="1-初识"><a href="#1-初识" class="headerlink" title="1. 初识"></a>1. 初识</h2><p>最近公司项目遇到一个比较奇怪的问题，现象是在我们的系统（centos6）中发的请求包，总是需要延时5s才能成功. 一想到超时，第一感觉应该就是网络差，于是通过<code>curl www.baidu.com</code>验证，发现也是延时5s才返回，因此第一个结论：网络差</p>
<p>为了进一步确认，尝试在同一网络下其他PC机（Windows）上访问百度网页，发现比较快，并确定不是缓存，于是就有了第一个疑问，”为什么相同网络下不同PC访问百度的时间差别这么大？”</p>
<h2 id="2-误打误撞"><a href="#2-误打误撞" class="headerlink" title="2. 误打误撞"></a>2. 误打误撞</h2><p>针对第一个疑问，对比了PC机的网络配置，是完全一样的，这就无法解释了。就在没有方向的时候，尝试用IP访问，<code>curl 163.177.151.109</code>，发现速度很快，突然想到我们的请求也是使用域名. 于是自然就转向了DNS，是不是DNS解析慢，于是尝试更换了几个DNS（114.114.114.114、8.8.8.8），可是发现问题依旧.</p>
<h2 id="3-DNS充电"><a href="#3-DNS充电" class="headerlink" title="3. DNS充电"></a>3. DNS充电</h2><p>既然问题已经定位在与DNS有关，于是我查阅了资料，了解了DNS的工作流程，如下：</p>
<p><img src="/images/dns-interaction.png" alt="image"></p>
<ul>
<li>递归查询 - A找B要东西；B发现自己没有，B找C要；C给B；B再给A</li>
<li>迭代查询 - A找B要东西；B发现自己没有于是告知A，C有；A找C要，C给A</li>
</ul>
<p>递归查询一般用于本地DNS服务器，迭代查询用于DNS服务器之间</p>
<p>言归正传，于是我使用<code>nslook www.baidu.com</code>发现可以快速返回，因此猜测DNS可以正常获取</p>
<h2 id="4-再抓包"><a href="#4-再抓包" class="headerlink" title="4. 再抓包"></a>4. 再抓包</h2><p>为了找出原因，我使出了杀手锏，用tcpdump抓包，<code>tcpdump -nn host 114.114.114.114</code></p>
<pre>
00:27:09.459055 IP 192.168.1.88.49776 > 114.114.114.114.53: 33750+ A? www.baidu.com. (31)
00:27:09.459067 IP 192.168.1.88.49776 > 114.114.114.114.53: 46955+ AAAA? www.baidu.com. (31)
00:27:09.482865 IP 114.114.114.114.53 > 192.168.1.88.49776: 33750 3/0/0 CNAME www.a.shifen.com., A 14.215.177.37, A 14.215.177.38 (90)
00:27:14.459055 IP 192.168.1.88.49776 > 114.114.114.114.53: 33750+ A? www.baidu.com. (31)
00:27:14.482865 IP 114.114.114.114.53 > 192.168.1.88.49776: 33750 3/0/0 CNAME www.a.shifen.com., A 14.215.177.37, A 14.215.177.38 (90)
00:27:14.459067 IP 192.168.1.88.49776 > 114.114.114.114.53: 46955+ AAAA? www.baidu.com. (31)
00:27:14.482898 IP 114.114.114.114.53 > 192.168.1.88.49776: 46955 1/1/0 CNAME www.a.shifen.com. (115)
</pre>
发现客户端发起A、AAAA的查询，但服务端只回了A，没有回AAAA，于是客户端等待超时后，继续单条单条查询，成功返回. 为了对比，我们再正常PC上也抓包，结果如下：

<pre>
00:27:09.459055 IP 192.168.1.88.49776 > 114.114.114.114.53: 33750+ A? www.baidu.com. (31)
00:27:09.459067 IP 192.168.1.88.49776 > 114.114.114.114.53: 46955+ AAAA? www.baidu.com. (31)
00:27:09.482865 IP 114.114.114.114.53 > 192.168.1.88.49776: 33750 3/0/0 CNAME www.a.shifen.com., A 14.215.177.37, A 14.215.177.38 (90)
00:27:09.482898 IP 114.114.114.114.53 > 192.168.1.88.49776: 46955 1/1/0 CNAME www.a.shifen.com. (115)
</pre>

<p>因此初步断定，是由于无法获取到ipv6地址导致延时，于是通过<code>curl -4 www.baidu.com</code>发现确实没有延时. 可是为什么会获取不到ipv6呢，单条发送是可以正常返回，于是猜到可能是防火墙再搞鬼. 这其中正好发现了这篇《<a href="http://coolnull.com/3820.html" target="_blank" rel="noopener">centos 6中single-request-reopen参数说明</a>》文章</p>
<p><img src="http://static.coolnull.com/wp-content/uploads/2015/05/v3.png" alt="image"></p>
<h2 id="5-结论"><a href="#5-结论" class="headerlink" title="5. 结论"></a>5. 结论</h2><p>综上所述，最终原因出在了防火墙上，但目前防火墙配置不在我们这边，暂时还无法得知，但应该基本可以确定是防火墙搞的鬼，具体的解决方案<code>在/etc/resolv.conf添加以下参数options single-request-reopen</code></p>
<h2 id="6-总结"><a href="#6-总结" class="headerlink" title="6. 总结"></a>6. 总结</h2><p>以上是描述了我对”为何请求总是延时5s”问题的追踪过程，从中学到了DNS解析流程、dig命令，可谓甚丰.</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/01/13/2017-01-13-SIP信令裁剪/" rel="next" title="SIP信令裁剪">
                <i class="fa fa-chevron-left"></i> SIP信令裁剪
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/03/15/2017-03-15-解决Github Page无法更新/" rel="prev" title="解决Github Page无法更新">
                解决Github Page无法更新 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">smallmuou</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">197</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/smallmuou" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-初识"><span class="nav-number">1.</span> <span class="nav-text">1. 初识</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-误打误撞"><span class="nav-number">2.</span> <span class="nav-text">2. 误打误撞</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-DNS充电"><span class="nav-number">3.</span> <span class="nav-text">3. DNS充电</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-再抓包"><span class="nav-number">4.</span> <span class="nav-text">4. 再抓包</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-结论"><span class="nav-number">5.</span> <span class="nav-text">5. 结论</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-总结"><span class="nav-number">6.</span> <span class="nav-text">6. 总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">smallmuou</span>

  
</div>

    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <span id="busuanzi_container_site_pv">总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
